﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;
using MySql.Data.MySqlClient;
using System.Data;
using l1cs.server.model;
using l1cs.server.datatables;
using l1cs.server.utils;

namespace l1cs.server.clientpackets
{
    public class C_CommonClick
    {
        private const String _C_COMMON_CLICK = "[C] C_CommonClick";

        private static Logger _log = Logger.getLogger( typeof( C_CommonClick ).FullName );

        public C_CommonClick( ClientThread client )
        {
            deleteCharacter( client ); // 削除期限に達したキャラクターを削除する
            Account account = client.getAccount();
            if ( account != null )
            {
                int amountOfChars = account.countCharacters();
                client.sendPacket( new S_CharAmount( amountOfChars, client ) );
                if ( amountOfChars > 0 )
                {
                    sendCharPacks( client );
                }
            }
        }

        private void deleteCharacter( ClientThread client )
        {
            String sqlstr = @"SELECT * FROM characters WHERE account_name=?1 ORDER BY objid";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.String ).Value = client.getAccountName();
                foreach ( DataRow item in wrapper.GetDataTable( cmd ).Rows )
                {
                    String name = item["char_name"] as String;
                    String clanname = item["Clanname"] as String;

                    Nullable<DateTime> deleteTime = SqlUtils.toDateTime( item["DeleteTime"] );
                    if ( deleteTime == null )
                    {
                        continue;
                    }
                    int checkDeleteTime = ( DateTime.Now - (DateTime)deleteTime ).Days;
                    if ( checkDeleteTime >= 0 )
                    {
                        L1Clan clan = L1World.getInstance().getClan( clanname );
                        if ( clan != null )
                        {
                            clan.delMemberName( name );
                        }
                        CharacterTable.getInstance().deleteCharacter( client.getAccountName(), name );
                    }
                }
            }
        }

        private void sendCharPacks( ClientThread client )
        {
            String sqlstr = "SELECT * FROM characters WHERE account_name=?1 ORDER BY objid";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.String ).Value = client.getAccountName();
                foreach ( DataRow item in wrapper.GetDataTable( cmd ).Rows )
                {
                    String name = item["char_name"] as String;
                    String clanname = item["Clanname"] as String;
                    int type = SqlUtils.toInt( item["Type"] );
                    byte sex = Convert.ToByte( item["Sex"] );
                    int lawful = SqlUtils.toInt( item["Lawful"] );

                    int currenthp = SqlUtils.toInt( item["CurHp"] );
                    if ( currenthp < 1 )
                    {
                        currenthp = 1;
                    }
                    else if ( currenthp > 32767 )
                    {
                        currenthp = 32767;
                    }

                    int currentmp = SqlUtils.toInt( item["CurMp"] );
                    if ( currentmp < 1 )
                    {
                        currentmp = 1;
                    }
                    else if ( currentmp > 32767 )
                    {
                        currentmp = 32767;
                    }

                    int lvl;
                    if ( Config.CHARACTER_CONFIG_IN_SERVER_SIDE )
                    {
                        lvl = SqlUtils.toInt( item["level"] );
                        if ( lvl < 1 )
                        {
                            lvl = 1;
                        }
                        else if ( lvl > 127 )
                        {
                            lvl = 127;
                        }
                    }
                    else
                    {
                        lvl = 1;
                    }

                    int ac = Convert.ToSByte( item["Ac"] );
                    int str = Convert.ToByte( item["Str"] );
                    int dex = Convert.ToByte( item["Dex"] );
                    int con = Convert.ToByte( item["Con"] );
                    int wis = Convert.ToByte( item["Wis"] );
                    int cha = Convert.ToByte( item["Cha"] );
                    int intel = Convert.ToByte( item["Intel"] );
                    int accessLevel = SqlUtils.toShort( item["AccessLevel"] );

                    S_CharPacks cpk = new S_CharPacks( name, clanname, type, sex,
                            lawful, currenthp, currentmp, ac, lvl, str, dex, con,
                            wis, cha, intel, accessLevel );

                    client.sendPacket( cpk );
                }
            }
        }

        public String getType()
        {
            return _C_COMMON_CLICK;
        }
    }
}